// pages/detail/detail.js
Page({

  /**
   * 页面的初始数据
   */

  // toggleCollect
  // searchCollect
  data: {
    title: "",
    obj: {},
    fangyiFlat: false,
    shangxiFlat: false,
    descFlat: false,
    poet: "",
    collectFlat: false
  },
  toggleFlat4() {
    let that = this
    return new Promise((reslove, reject) => {
      if (wx.getStorageSync('openid')) {
        wx.request({
          url: 'http://localhost:3000/user/toggleCollect',
          method: "post",
          data: {
            poetry_name: wx.getStorageSync('msg'),
            poet_name: wx.getStorageSync('poetName'),
            user_id: wx.getStorageSync('openid'),
          },
          success(data2) {
            // console.log(data.data)
            wx.showToast({
              title: data2.data.msg,
            })
            that.setData({
              collectFlat: !that.data.collectFlat
            })
            reslove({
              code: 200,
              msg: "切换成功"
            })
          }
        })
      } else {
        wx.showToast({
          title: '未登录无法收藏',
          icon: "error"
        })
        reslove({
          code: 200,
          msg: "未登录无法收藏"
        })
      }

    })

  },
  async changeFlat() {

    await this.toggleFlat4()
  },
  searchCollect() {
    let that = this;
    return new Promise((reslove, reject) => {
      wx.request({
        url: 'http://localhost:3000/user/searchCollect',
        method: "POST",
        data: {
          poetry_name: wx.getStorageSync('msg'),
          poet_name: wx.getStorageSync('poetName'),
          user_id: wx.getStorageSync('openid'),
        },
        success(data) {
          console.log(data.data.flat)
          that.setData({
            collectFlat: data.data.flat
          })
          reslove({
            code: 200,
            msg: "获取收藏记录成功"
          })
        }
      })
    })

  },
  toggleFlat() {
    this.setData({
      fangyiFlat: !this.data.fangyiFlat
    })
  },
  toggleFlat2() {
    this.setData({
      shangxiFlat: !this.data.shangxiFlat
    })
  },
  toggleFlat3() {
    this.setData({
      descFlat: !this.data.descFlat
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  getDetail() {
    let that = this
    return new Promise((reslove, reject) => {
      wx.request({
        url: 'http://localhost:3000/poetry/poetryDetail',
        method: "POST",
        data: {
          name: this.data.title,
          poetName: this.data.poetName
        },
        success(data) {
          console.log(data.data.rows)
          data.data.rows.map((item) => {

            if (item.fanyi.indexOf("译文") == 1) {
              item.fanyi = item.fanyi.substr(4)
            } else if (item.fanyi == "undefined") {
              item.fanyi = ""
            } else {
              item.fanyi = item.fanyi.substr(3)
            }

            if (item.shangxi.indexOf("赏析") == 0) {
              item.shangxi = item.shangxi.substr(4)
            } else if (item.shangxi.indexOf("赏析") > 0) {
              let idx = item.shangxi.indexOf("赏析")
              item.shangxi = item.shangxi.substr(5 + idx)
              // console.log(item.shangxi.substr(0, 1) == "　")
            } else if (item.shangxi.indexOf("赏析") == -1 && item.shangxi.length > 10) {

              item.shangxi = item.shangxi.substr(66)
            } else if (item.shangxi.indexOf("赏析") == -1 && item.shangxi.length < 10) {
              item.shangxi = ""
            }

            if (item.shangxi.substr(0, 2) != "　　" && item.shangxi.length > 10) {
              item.shangxi = "　" + item.shangxi
            }
            if (item.fanyi.substr(0, 2) != "　　" && item.fanyi.length > 10) {
              item.fanyi = "　　" + item.fanyi
            }

            item.poet = JSON.parse(item.poet);
            console.log(item.poet)
            if (item.poet.desc) {
              if (item.poet.desc.substr(0, 2) != "　　" && item.poet.desc.length > 10) {
                item.poet.desc = "　　" + item.poet.desc
              }
            }

            item.tags = JSON.parse(item.tags)
            console.log(item.shangxi.indexOf("赏析"), 5)

          })
          that.setData({
            obj: data.data.rows[0]
          })
          reslove({
            code: 200,
            msg: "获取成功"
          })
        }
      })
    })

  },
  /**
   * 生命周期函数--监听页面显示
   */
  addWeb() {
    return new Promise((reslove, reject) => {
      wx.request({
        url: 'http://localhost:3000/user/addWeb',
        method: "POST",
        data: {
          poetry_name: wx.getStorageSync('msg'),
          poet_name: wx.getStorageSync('poetName'),
          user_id: wx.getStorageSync('openid'),
        },
        success(data) {
          console.log(data)
          reslove({
            code: 200,
            msg: "获取成功"
          })
        }
      })
    })

  },
  async onShow() {
    // console.log(wx.getStorageSync('msg'));
    // console.log(wx.getStorageSync('poetName'));
    wx.showLoading({
      title: '加载中',
    })
    this.setData({
      title: wx.getStorageSync('msg'),
      poetName: wx.getStorageSync('poetName')
    })
    await this.getDetail()
    if (wx.getStorageSync('openid')) {
      await this.addWeb()
      await this.searchCollect()
    }
    wx.hideLoading()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})